<template>
  <div>

    <el-form :inline="false" :model="res" :rules="rules" ref="ruleForm" label-width="140px" style="padding-bottom: 10px;width:460px">
      <el-form-item label="原始密码：" required prop="accountPassword">
        <el-input placeholder="请输入当前账户密码" type="password" v-model="res.accountPassword" clearable></el-input>
      </el-form-item>
      <el-form-item label="新密码：" required prop="newPassword">
        <el-input placeholder="请输入新密码" type="password" v-model="res.newPassword" clearable></el-input>
      </el-form-item>
      <el-form-item label="再次输入新密码：" required prop="newToPassword">
        <el-input placeholder="请再次输入新密码" type="password" v-model="res.newToPassword" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-success" @click="submit">修改</el-button>
        <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
        
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  export default {
    data () {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入新密码'))
        } else if (value !== this.res.newPassword) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      }
      return {
        res: {
          accountName: this.$sess.get('userInfo').accountName,
          accountPassword: '',
          newPassword: '',
          newToPassword: ''
        },
        rules: {
          accountPassword: [
            {
              required: true,
              message: '请输入原始密码',
              trigger: 'blur'
            },
            {
              pattern: '[A-Za-z0-9]{6,200}',
              message: '只支持字母、数字、大于等于6位',
              trigger: 'blur'
            }
          ],
          newPassword: [
            {
              required: true,
              message: '请输入新密码',
              trigger: 'blur'
            },
            {
              pattern: '[A-Za-z0-9]{6,200}',
              message: '只支持字母、数字、大于等于6位',
              trigger: 'blur'
            }
          ],
          newToPassword: [{
            validator: validatePass,
            trigger: 'change'
          }]
        }
      }
    },
    methods: {
      submit () {
        this.$refs['ruleForm'].validate((valid) => {
          if (valid) {
            window.instance.get('/controller/updatePwd', {
              params: this.res
            }).then((res) => {
              if (res && res.data && ~~res.data.code === 1) {
                this.warning('修改成功', 'success')
                window.history.go(-1)
              } else {
                this.warning('修改失败' + res && res.data && res.data.msg)
              }
            })
          } else {
            return false
          }
        })
      },
      reset () {
        this.$refs['ruleForm'].resetFields()
      },
      warning (val, state = 'warning') {
        this.$message({
          message: val,
          type: state
        })
        return false
      }
    },
    careted () {

    }
  }
</script>